package com.example.wiki.service;


import com.example.wiki.domain.Ebook;
import com.example.wiki.domain.EbookExample;
import com.example.wiki.mapper.EbookMapper;
import com.example.wiki.req.EbookQueryReq;
import com.example.wiki.resp.EbookQueryResp;
import com.example.wiki.resp.PageResp;
import com.example.wiki.utils.CopyUtil;
import com.example.wiki.utils.SnowFlake;
import com.example.wiki.req.EbookSaveReq;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;

import java.util.List;


@Service
public class EbookService {
    private static final Logger Log = LoggerFactory.getLogger(EbookService.class);
    @Resource
    private EbookMapper ebookMapper;

    @Resource
    private SnowFlake snowFlake;

    /**
     * 查询列表
     * @param req
     * @return
     */
    public PageResp<EbookQueryResp> list(EbookQueryReq req) {



        EbookExample ebookExample = new EbookExample();
        EbookExample.Criteria criteria = ebookExample.createCriteria();
        if (!ObjectUtils.isEmpty(req.getName())) {
            criteria.andNameLike("%" + req.getName() + "%");
        } else if (!ObjectUtils.isEmpty(req.getCategory2Id())) {
            criteria.andCategory2IdEqualTo(+req.getCategory2Id());
            
        }


        PageHelper.startPage(req.getPage(),req.getSize());
        List<Ebook> ebookList = ebookMapper.selectByExample(ebookExample);

        PageInfo<Ebook> pageInfo = new PageInfo<>(ebookList);
        Log.info("总行数"+pageInfo.getTotal());
        Log.info("总页数"+pageInfo.getPages());

//        List<EbookResp> respList  =new ArrayList<>();
//        for (Ebook ebook :ebookList){
//
//            EbookResp ebookResp = new EbookResp();
//            //最原始的设置属性值方法
////            ebookResp.setName(ebook.getName());
////            利用beanutil的简便方法
////            BeanUtils.copyProperties(ebook,ebookResp);
////            respList.add(ebookResp);
//        }
        //利用工具封装，简化上面代码

        List<EbookQueryResp> respList = CopyUtil.copyList(ebookList, EbookQueryResp.class);

        PageResp<EbookQueryResp> pageResp =new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(respList);

        return pageResp;
    }


        /**
         * 保存
         */


        public void save(@RequestBody EbookSaveReq req) {
            System.out.println(req);
            Ebook ebook = CopyUtil.copy(req, Ebook.class);
            if (ObjectUtils.isEmpty(req.getId())) {
                // 新增  自增 uuid 雪花
                ebook.setId(snowFlake.nextId());
                ebook.setVoteCount(0);
                ebook.setViewCount(0);
                ebook.setDocCount(0);
                ebookMapper.insert(ebook);
            } else {
                // 更新
                ebookMapper.updateByPrimaryKey(ebook);
            }
        }

        public void delete(Long id){
            ebookMapper.deleteByPrimaryKey(id);
        }


    }

